# -*- coding: utf-8 -*-
from django.views.generic.base import TemplateView
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth import authenticate, login, logout
from django.views import View
from django.urls import reverse
from django.shortcuts import redirect, render
from django.contrib.auth import (
    REDIRECT_FIELD_NAME
)
from django.utils.http import is_safe_url, urlsafe_base64_decode
from django.http import HttpResponseRedirect, QueryDict
from django.contrib import messages
from . import dao
from .models import Genre
# Create your views here.


class LoginView(TemplateView):
    template_name = "account/login.html"

    def get(self, req):
        redirect_field_name = REDIRECT_FIELD_NAME
        next_page = None
        if (redirect_field_name in req.POST or
                redirect_field_name in req.GET):
            next_page = req.POST.get(redirect_field_name,
                                     req.GET.get(redirect_field_name))
        if not is_safe_url(url=next_page, allowed_hosts=req.get_host()):
            next_page = None  # req.path
        if not next_page:
            print(req.META)
            next_page = req.META['HTTP_REFERER']
        return render(req, self.template_name, {'next': next_page})

    def post(self, req):
        username = req.POST['username']
        password = req.POST['password']
        user = authenticate(username=username, password=password)
        #obj_user = dao.get_user(username)
        if user is not None:
            login(req, user)
            redirect_field_name = REDIRECT_FIELD_NAME
            if (redirect_field_name in req.POST or
                    redirect_field_name in req.GET):
                next_page = req.POST.get(redirect_field_name,
                                         req.GET.get(redirect_field_name))
            # Security check -- don't allow redirection to a different host.
            if not is_safe_url(url=next_page, allowed_hosts=req.get_host()):
                next_page = None  # req.path

            if next_page:
                return HttpResponseRedirect(next_page)
            else:
                url = reverse('home')
                return redirect(url)
        else:
            return render(req, self.template_name, {'error': '用户名密码不正确', 'username': username})


class JoinView(TemplateView):
    template_name = 'account/join.html'

    def post(self, req):
        username = req.POST['username']
        password = req.POST['password']
        email = username  # req.POST['email']
        u = dao.new_user(username, password, email)
        if u:
            login(req, u)
            url = reverse('twitter:home')
            return redirect(url)
        else:
            return render(req, self.template_name, {'error': '用户已存在', 'username': username})


class LogoutView(View):
    def get(self, req):
        logout(req)
        messages.add_message(req, messages.INFO, 'You were logged out')
        url = reverse('twitter:home')
        return redirect(url)



def show_genres(req):
    return render(req, "genres.html", {'nodes': Genre.objects.all()})
